package com.heima;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
@WebServlet("/login")
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取客户端的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        //获取文件存储路径
        String path = this.getServletContext().getRealPath("/b.txt");
        //从本地获取user对象
        BufferedReader br=new BufferedReader(new FileReader(path));
        String s;
        while ((s=br.readLine())!=null){
            System.out.println(s);
            String[] split = s.split(",");
            String s1 = split[0];
            String s2 = split[1];
            if(username.equals(s1)&&password.equals(s2)){
                System.out.println("登录成功！！！");
                if(br!=null){br.close();}
                resp.sendRedirect("/loginSucsseful.html");
                return;
            }
        }
        resp.sendRedirect("/login.html");
        if(br!=null){br.close();}
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
